<template>
  <div class="login">
    <!-- 登录 -->
    <div class="login-wrap">
      <div class="login">
        <div class="loginform">
          <ul class="tab clearFix">
            <li>
              <a href="##" class="current">账户登录</a>
            </li>
          </ul>

          <div class="content">
            <el-form :model="loginForm" :rules="rules" ref="loginForm">
              <el-form-item class="input-text clearFix" prop="account">
                <i></i>
                <el-input
                  type="text"
                  placeholder="请输入账号"
                  v-model="loginForm.account"
                />
              </el-form-item>

              <el-form-item class="input-text clearFix" prop="password">
                <i class="pwd"></i>
                <el-input
                  type="password"
                  placeholder="请输入密码"
                  v-model="loginForm.password"
                />
              </el-form-item>

              <el-form-item class="setting">
                <el-checkbox-group v-model="loginForm.autoplay">
                  <el-checkbox
                    label="自动登录"
                    name="loginForm.autoplay"
                  ></el-checkbox>
                </el-checkbox-group>
                <span class="forget">忘记密码？</span>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  class="btn"
                  @click="submitForm('loginForm')"
                  >登&nbsp;&nbsp;录</el-button
                >
              </el-form-item>
            </el-form>
            <div class="call clearFix">
              <router-link to="/register" class="register">立即注册</router-link>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 底部 -->
    <div class="copyright">
      <ul>
        <li>关于我们</li>
        <li>联系我们</li>
        <li>联系客服</li>
        <li>商家入驻</li>
        <li>营销中心</li>
        <li>手机尚品汇</li>
        <li>销售联盟</li>
        <li>尚品汇社区</li>
      </ul>
      <div class="address">地址：北京市昌平区宏福科技园综合楼6层</div>
      <div class="beian">京ICP备19006430号</div>
    </div>
  </div>
</template>
<script>
import { reqUserLogin } from "@/api";
import { setToken } from "@/utils/token";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
        autoplay: true,
      },
      rules: {
        account: { required: true, message: "请输入账号", trigger: "blur" },
        password: { required: true, message: "请输入密码", trigger: "blur" },
      },
    };
  },
  //页面加载调用获取cookie值
  mounted(){
    this.getCookie()
    console.log(this.$route.query.redirect)
  },
  methods: {
    submitForm(formName) {
      // 存入cookie
      if(this.loginForm.autoplay == true){
        
        this.setCookie(this.loginForm.account, this.loginForm.password, 7);
      }else{
        this.clearCookie()
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = {
            phone: this.loginForm.account,
            password: this.loginForm.password,
          };
          
          reqUserLogin(data)
            .then((res) => {
              if (res.code == 200) {
                
                this.$store.dispatch("userToken", { token: res.data.token });
                setToken(res.data.token);
                
                this.$message({
                  message: res.message,
                  type: "success",
                });
                
                let toPath = this.$route.query.redirect || "/";
                setTimeout( () => {
                  this.$router.push(toPath);
                },1000)
              } else if (res.code == 207) {
                this.$message({
                  message: res.message,
                  type: "warning",
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 存储cookie
    setCookie(account,pwd,exdays) {
      // 账号，密码， 过期时间
      let exdate = new Date(); //获取时间
      exdate.setTime(exdate.getTime() + 24*60*60*1000*exdays); //保存的天数
      // 字符串拼接 cookie
      window.document.cookie = "userName" + "=" + account +";path=/;expires=" + exdate.toGMTString();
      window.document.cookie = "password" + "=" + pwd +";path=/;expires=" + exdate.toGMTString();
    },
    // 读取cookie
    getCookie(){
      if(document.cookie.length > 0) {
        let arr = document.cookie.split(';'); // 显示格式需要切割一下
        for( let i = 0; i < arr.length; i++) {
          let arr2 = arr[i].split('='); // 再次切割
          // 判断查找相对应的值
          let newArr = arr2[0].replace(/\s*/g,"")
          if(newArr == 'userName'){
            this.loginForm.account = arr2[1]; //保存到保存数据的地方
          }else if(newArr == 'password'){
            this.loginForm.password = arr2[1];
          }
        }
      }
    },
    // 清除cookie
    clearCookie() {
      this.setCookie('','',-1) 
    }
  },
};
</script>
<style lang="less" scoped>
.login-wrap {
  height: 487px;
  background-color: #e93854;
  .login {
    width: 1200px;
    height: 487px;
    margin: 0 auto;
    background: url(~@/assets/images/loginbg.png) no-repeat;
  }
  .loginform {
    width: 420px;
    height: 406px;
    box-sizing: border-box;
    background: #fff;
    float: right;
    top: 45px;
    position: relative;
    padding: 20px;
    .tab {
      li {
        width: 100%;
        float: left;
        text-align: center;
        a {
          width: 100%;
          display: block;
          height: 50px;
          line-height: 50px;
          font-size: 20px;
          font-weight: 700;
          color: #333;
          border: 1px solid #ddd;
          box-sizing: border-box;
          text-decoration: none;
        }
        .current {
          border-bottom: none;
          border-top-color: #28a3ef;
          color: #e1251b;
        }
      }
    }
    .content {
      width: 380px;
      height: 316px;
      box-sizing: border-box;
      border: 1px solid #ddd;
      border-top: none;
      padding: 18px;
      /deep/ .el-form {
        margin: 15px 0 18px 0;
        font-size: 12px;
        line-height: 18px;
        .el-form-item__content {
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
        .input-text {
          margin-bottom: 22px;
          position: relative;
          i {
            float: left;
            width: 37px;
            height: 32px;
            border: 1px solid #ccc;
            background: url(~@/assets/images/icons.png) no-repeat -10px -201px;
            box-sizing: border-box;
            border-radius: 2px 0 0 2px;
          }
          .pwd {
            background-position: -72px -201px;
          }
          input {
            width: 302px;
            height: 32px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-left: none;
            float: left;
            padding-top: 6px;
            padding-bottom: 6px;
            font-size: 14px;
            line-height: 22px;
            padding-right: 8px;
            padding-left: 8px;

            border-radius: 0 2px 2px 0;
            outline: none;
          }

          .error-msg {
            position: absolute;
            top: 100%;
            left: 40px;
            color: red;
          }
        }
        .setting {
          margin-bottom: 0;
        }
        .btn {
          background-color: #e1251b;
          padding: 6px;
          border-radius: 0;
          font-size: 16px;
          font-family: 微软雅黑;
          word-spacing: 4px;
          border: 1px solid #e1251b;
          color: #fff;
          width: 100%;
          height: 36px;
          margin-top: 25px;
          outline: none;
        }
      }
      .call {
        margin-top: 30px;
        ul {
          float: left;
          li {
            float: left;
            margin-right: 5px;
          }
        }
        .register {
          float: right;
          font-size: 15px;
          line-height: 38px;
        }
        .register:hover {
          color: #4cb9fc;
          text-decoration: underline;
        }
      }
    }
  }
}
.copyright {
  width: 1200px;
  margin: 0 auto;
  text-align: center;
  line-height: 24px;
  ul {
    li {
      display: inline-block;
      border-right: 1px solid #e4e4e4;
      padding: 0 20px;
      margin: 15px 0;
    }
  }
}
</style>